<?php
namespace app\admin\controller;
use app\admin\model\BrandBusiness;
use think\Request;

class Brand extends Base
{
    protected function _initialize(){
        $this->checkLogin();
        $this->menuLoad();
    }
    public function index(){
        return $this->fetch('public/help');
    }

    public function brandList(){
        $brand = new \app\admin\model\Brand();
        $where['id'] = ['neq',0];
        $rs = $brand->getAll($where);
        $this->assign('rs',$rs);
        return $this->fetch();
    }

    public function addBrand(Request $request){
        if($request->post()){
            $data = $request->post();
            $brand = new \app\admin\model\Brand();
            if($data["name"] == '' || $data["sort"] == '' || $data["img"] == '' || $data["status"] == '' || $data["describe"] == '')return $this->error('参数不完整');
           $rs = $brand->add($data);
            if($rs){
                return $this->success('添加成功');
            }else{
                return $this->error('添加失败');
            }
        }
        return $this->fetch();
    }

    public function editBrand(Request $request){
        $brand = new \app\admin\model\Brand();
        if($request->post()){
            $data = $request->post();
            if($data["name"] == '' || $data["sort"] == '' || $data["img"] == '' || $data["status"] == '' || $data["describe"] == '')return $this->error('参数不完整');
            $where['id'] = $data['id'];
            unset($data['id']);
            $rs = $brand->edit($where,$data);
            if($rs){
                return $this->success('修改成功');
            }else{
                return $this->error('修改失败');
            }
        }else{
            $where['id'] = $request->get('id');
            $rs = $brand->getOne($where);
            $this->assign('rs',$rs);
        }
        return $this->fetch();
    }

    public function businessList(Request $request){
        $data = $request->get();
        $brandBusiness = new BrandBusiness();
        $where['brand_id'] = $data['id'];
        $rs = $brandBusiness->getAll($where);
        $this->assign('rs',$rs);
        $this->assign('brand_id',$data['id']);
        return $this->fetch();
    }

    public function addBusiness(Request $request){
        if($request->post()){
            $data = $request->post();
            $brandBusiness = new BrandBusiness();
            $rs = $brandBusiness->add($data);
            if($rs){
                return $this->success('添加成功');
            }else{
                return $this->error('添加失败');
            }
        }
        $this->assign('brand_id',$request->get('brand_id'));
        return $this->fetch();
    }

    public function editBusiness(Request $request){
        $brandBusiness = new BrandBusiness();
        if($request->post()){
            $data = $request->post();
            if($data["name"] == '' || $data["describe"] == '' || $data["id"] == '' || $data["sort"] == '')return $this->error('参数不完整');
            $where['id'] = $data['id'];
            unset($data['id']);
            $rs = $brandBusiness->edit($where,$data);
            if($rs){
                return $this->success('修改成功');
            }else{
                return $this->error('修改失败');
            }
        }else{
            $where['id'] = $request->get('id');
            $rs = $brandBusiness->getOne($where);
            $this->assign('rs',$rs);
        }
        return $this->fetch();
    }

}